/**
 * 电梯风险监测统计看板
 */
odoo.define('lift_risk.risk_watch', function (require) {
    "use strict";
    var ajax = require('web.ajax');
    var core = require('web.core');
    var Widget = require('web.Widget');
    var Model = require('web.Model');
    var session = require('web.session');
    var framework = require('web.framework');
    var form_common = require('web.form_common')
    var list_widget_registry = core.list_widget_registry;
    var ListView = require("web.ListView");
    var _t = core._t;

    var LiftCommon = require("lift_base.common_widget");
    var CommonInfoBoxWidget = LiftCommon.CommonInfoBoxWidget;
    var CommonInfoBoxWithProgressWidget = LiftCommon.CommonInfoBoxWithProgressWidget;


    var get_last_month = function () {
        var current_date = new Date();
        var year = current_date.getFullYear();
        var month = current_date.getMonth();
        if(month == 0){
            month=12;
            year=year-1;
        } else if (month < 10) {
            month = "0" + month;
        }
        return year + "-" + month + "-" + '15'
    }

    //按得分排序显示的记录列表
    var RiskOrderBoxRowWidget = Widget.extend({
        template:"tyibs_risk_order_box_row_template",
        events:{
            "click a.tyibs-risk-order-box-row-name":"show_brand_risk_form"
        },
        init:function(parent, res_model, item, index ){
            this._super(parent);
            this.item = item;
            this.index = index;
            this.res_model = res_model;
        },
        //显示某个具体的风险表单
        show_brand_risk_form:function(){
            var self = this;
            self.do_action({
                name: self.item.name,
                type: 'ir.actions.act_window',
                res_model: self.res_model,
                views: [[false, 'form']],
                res_id: self.item.id,
                target: 'current',
            });
            return false;
        }
    });

    //显示排序记录的盒子
    var RiskOrderBoxWidget = Widget.extend({
        template:"tyibs_risk_order_box_template",
        events:{
            "click a.tyibs-risk-order-box-more-button":"show_risk_list"
        },
        init:function(parent, title, res_model, order_field, name_field, limit_order_count){
            this._super(parent);
            this.title = title;
            this.records = [];
            this.order_field = order_field;
            this.name_field = name_field;
            this.limit_order_count = limit_order_count;
            this.model = new Model(res_model);
            // this.set_last_month();
        },
        start:function(){
            var self = this;
            var row_container = self.$("div.tyibs-risk-order-box-row-head");
            self.get_format_order_data().then(function(records){
                records.forEach(function(item, index){
                    var row_widget = new RiskOrderBoxRowWidget(self, self.model.name, item, index);
                    row_widget.insertBefore(row_container);
                });
                //默认情况下会将标题行挤到最下面，下面的代码把标题行放到第一行去
                var row_header = self.$("div.tyibs-risk-order-box-row-head").first();
                row_header.remove();
                self.$("div.list-group-item").first().before(row_header);
            });
        },
        get_order_data:function(){
            var self = this;
            return self.model.query([self.name_field,self.order_field])
                .filter([['risk_date', '=', get_last_month()]])
                .order_by("-"+self.order_field)
                .limit(self.limit_order_count)
                .all();
        },
        //统一格式化为 name, score, id的数据格式
        get_format_order_data:function(){
            var self = this;
            var def = $.Deferred();
            this.get_order_data().then(function(records){
                records.forEach(function(item){
                    item.score = item[self.order_field];
                    item.name = item[self.name_field][1];
                    //item.name = item.name.substr(0,16);
                });
                def.resolve(records);
            });
            return def.promise();
        },
        //点击更多时显示的风险列表
        show_risk_list:function(){
            var self = this;
            self.do_action({
                name: self.title,
                type: 'ir.actions.act_window',
                res_model: self.model.name,
                views: [[false, 'list'],[false, 'form']],
                target: 'current',
                //排序暂时还不起作用
                context: {
                    'order_by': self.order_field + " desc"
                },
            });
            return false;
        },

    });

    //电梯品牌风险监测
    var RiskBrandIndex = Widget.extend({
        template:"risk_brand_watch_index",
        init:function(parent){
            this._super(parent);
            this.max_score = 60;
            this.limit_order_count = 6;
            this.model = new Model("tyibs.risk.brand");
            this.description = "评分低于"+ this.max_score + "分";
        },
        start:function(){
            this.show_box_list();
            this.show_order_box();
        },
        show_order_box:function(){
            var container = this.$("div.tyibs-risk-order-box-container");

            var order_complex_widget = new RiskOrderBoxWidget(this,"综合评分排名",this.model.name, 'complex_score', 'factory_id',10);
            order_complex_widget.appendTo(container);

            var order_save_score_widget = new RiskOrderBoxWidget(this,"故障评分排名",this.model.name, 'save_score', 'factory_id',10);
            order_save_score_widget.appendTo(container);

            var order_consumer_score_widget = new RiskOrderBoxWidget(this,"客户评分排名",this.model.name, 'consumer_score', 'factory_id',10);
            order_consumer_score_widget.appendTo(container);
        },
        show_box_list:function(){
            var self = this;
            var box_container = this.$("div.tyibs-risk-box-container");

            self.get_box_count('complex_score').then(function(count){
                var complex_box = new CommonInfoBoxWithProgressWidget(self, "综合评分", count, "bg-blue", "fa-bookmark-o", "tyibs.risk.brand", [['complex_score','<',self.max_score]], self.description, 100);
                complex_box.appendTo(box_container);
                complex_box.on("show_risk_list", self, self.show_risk_list);
            });


            self.get_box_count('consumer_score').then(function(count){
                var consumer_box = new CommonInfoBoxWithProgressWidget(self, "客户评分", count, "bg-green", "fa-bookmark-o", "tyibs.risk.brand", [['consumer_score','<',self.max_score]], self.description, 100);
                consumer_box.appendTo(box_container);
                consumer_box.on("show_risk_list", self, self.show_risk_list);
            });

            self.get_box_count('save_score').then(function(count){
                var save_box = new CommonInfoBoxWithProgressWidget(self, "故障评分", count, "bg-red", "fa-star-o", "tyibs.risk.brand", [['save_score','<',self.max_score]], self.description, 100);
                save_box.appendTo(box_container);
                save_box.on("show_risk_list", self, self.show_risk_list);
            });

        },
        //根据条件获取条数
        get_box_count:function(score_field_name){
            var self = this;
            return this.model
                .query([])
                .filter([[score_field_name,'<', self.max_score], ['risk_date', '=', get_last_month()]])
                .count();
        },
        //点击盒子上的统计数据后显示电梯风险的列表
        show_risk_list:function(title, domain){
            var self = this;
            self.do_action({
                name: title,
                type: 'ir.actions.act_window',
                res_model: self.model.name,
                views: [[false, 'list'],[false, 'form']],
                target: 'current',
                domain:[ domain ],
                context: {},
            });
            return false;
        },
    });

    //维保单位风险监测
    var RiskMaintenanceIndex = Widget.extend({
        template:"risk_maintenance_watch_index",
        init:function(parent){
            this._super(parent);
            this.max_score = 60;
            this.model = new Model("tyibs.risk.maintenance.company");
            this.description = "评分低于"+ this.max_score + "分"; 
        },
        start:function(){
            this.show_box_list();
            this.show_order_box();
        },
        show_order_box:function(){
            var container = this.$("div.tyibs-risk-order-box-container");

            var order_complex_widget = new RiskOrderBoxWidget(this,"综合评分排名",this.model.name, 'complex_score', 'maintenance_company_id',6);
            order_complex_widget.appendTo(container);

            var order_check_score_widget = new RiskOrderBoxWidget(this,"检验评分排名",this.model.name, 'check_score', 'maintenance_company_id',6);
            order_check_score_widget.appendTo(container);

            var order_maintenance_widget = new RiskOrderBoxWidget(this,"维保评分排名",this.model.name, 'main_score', 'maintenance_company_id',6);
            order_maintenance_widget.appendTo(container);

            var order_save_score_widget = new RiskOrderBoxWidget(this,"故障评分排名",this.model.name, 'save_score', 'maintenance_company_id',6);
            order_save_score_widget.appendTo(container);

            var order_consumer_score_widget = new RiskOrderBoxWidget(this,"客户评分排名",this.model.name, 'consumer_score', 'maintenance_company_id',6);
            order_consumer_score_widget.appendTo(container);

        },
        show_box_list:function(){
            var self = this;
            var box_container = this.$("div.tyibs-risk-box-container");

            self.get_box_count('complex_score').then(function(count){
                var complex_box = new CommonInfoBoxWithProgressWidget(self, "综合评分", count, "bg-blue", "fa-bookmark-o", self.model.name, [['complex_score','<',self.max_score]], self.description, 100);
                complex_box.appendTo(box_container);
            });

            self.get_box_count('main_score').then(function(count){
                var maintenance_box = new CommonInfoBoxWithProgressWidget(self, "维保评分", count, "bg-aqua", "fa-bookmark-o", self.model.name, [['main_score','<',self.max_score]], self.description, 100);
                maintenance_box.appendTo(box_container);
            });

            self.get_box_count('check_score').then(function(count){
                var check_box = new CommonInfoBoxWithProgressWidget(self, "检验评分", count, "bg-yellow", "fa-bookmark-o", self.model.name, [['check_score','<',self.max_score]], self.description, 100);
                check_box.appendTo(box_container);
            });

            self.get_box_count('save_score').then(function(count){
                var save_box = new CommonInfoBoxWithProgressWidget(self, "故障评分", count, "bg-red", "fa-star-o", self.model.name, [['save_score','<',self.max_score]], self.description, 100);
                save_box.appendTo(box_container);
            });

            self.get_box_count('consumer_score').then(function(count){
                var consumer_box = new CommonInfoBoxWithProgressWidget(self, "客户评分", count, "bg-green", "fa-bookmark-o", self.model.name, [['consumer_score','<',self.max_score]], self.description, 100);
                consumer_box.appendTo(box_container);
            });

        },
        //根据条件获取条数
        get_box_count:function(score_field_name){
            var self = this;
            return this.model
                .query([])
                .filter([[score_field_name,'<', self.max_score], ['risk_date', '=', get_last_month()]])
                .count();
        },
    });

    //使用单位风险监测
    var RiskUseCompanyIndex = Widget.extend({
        template:"risk_use_company_watch_index",
        init:function(parent){
            this._super(parent);
            this.max_score = 60;
            this.model = new Model("tyibs.risk.use.company");
            this.description = "评分低于"+ this.max_score + "分";  
        },
        start:function(){
            this.show_box_list();
            this.show_order_box();
        },
        show_box_list:function(){
            var self = this;
            var box_container = this.$("div.tyibs-risk-box-container");

            self.get_box_count('complex_score').then(function(count){
                var complex_box = new CommonInfoBoxWithProgressWidget(self, "综合评分", count, "bg-blue", "fa-bookmark-o", self.model.name, [['complex_score','<',self.max_score]], self.description, 100);
                complex_box.appendTo(box_container);
            });

            self.get_box_count('check_score').then(function(count){
                var check_box = new CommonInfoBoxWithProgressWidget(self, "检验评分", count, "bg-yellow", "fa-bookmark-o", self.model.name, [['check_score','<',self.max_score]], self.description, 100);
                check_box.appendTo(box_container);
            });

            self.get_box_count('save_score').then(function(count){
                var save_box = new CommonInfoBoxWithProgressWidget(self, "故障评分", count, "bg-red", "fa-star-o", self.model.name, [['save_score','<',self.max_score]], self.description, 100);
                save_box.appendTo(box_container);
            });

            self.get_box_count('consumer_score').then(function(count){
                var consumer_box = new CommonInfoBoxWithProgressWidget(self, "客户评分", count, "bg-green", "fa-bookmark-o", self.model.name, [['consumer_score','<',self.max_score]], self.description, 100);
                consumer_box.appendTo(box_container);
            });
        },
        //根据条件获取条数
        get_box_count:function(score_field_name){
            var self = this;
            return this.model
                .query([])
                .filter([[score_field_name,'<', self.max_score], ['risk_date', '=', get_last_month()]])
                .count();
        },
        show_order_box:function(){
            var container = this.$("div.tyibs-risk-order-box-container");

            var order_complex_widget = new RiskOrderBoxWidget(this,"综合评分排名",this.model.name, 'complex_score', 'use_company_id',6);
            order_complex_widget.appendTo(container);

            var order_check_score_widget = new RiskOrderBoxWidget(this,"检验评分排名",this.model.name, 'check_score', 'use_company_id',6);
            order_check_score_widget.appendTo(container);

            var order_save_score_widget = new RiskOrderBoxWidget(this,"故障评分排名",this.model.name, 'save_score', 'use_company_id',6);
            order_save_score_widget.appendTo(container);

            var order_consumer_score_widget = new RiskOrderBoxWidget(this,"客户评分排名",this.model.name, 'consumer_score', 'use_company_id',6);
            order_consumer_score_widget.appendTo(container);
        },
    });

    core.action_registry.add('lift_risk.risk_brand_index', RiskBrandIndex);
    core.action_registry.add('lift_risk.risk_maintenance_index', RiskMaintenanceIndex);
    core.action_registry.add('lift_risk.risk_use_company_index', RiskUseCompanyIndex);
});